﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2013-12-05 18:20
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BassDrop.Core.Common.Cloud;
using BassDrop.Core.Common.Data.IO.Portable;
using BassDrop.Core.Common.Localization;
using BassDrop.Core.Common.Services;

namespace BassDrop.Core.Common
{
	/// <summary>
	/// Contains various configuration constants
	/// </summary>
	public static class Registry
	{
		/// <summary>
		/// Kind of a relative path
		/// </summary>
		public enum PathKind
		{
			/// <summary>
			/// Relative to the local system root
			/// </summary>
			LocalSystem,
			/// <summary>
			/// Relative to the local root of the current user
			/// </summary>
			LocalUser,
			/// <summary>
			/// Relative to the remote system root (in a cloud)
			/// </summary>
			RemoteSystem,
			/// <summary>
			/// Relative to the remote root of the current client application (in a cloud)
			/// </summary>
			RemoteClient
		}

		/// <summary>
		/// Relative path to the file of the currently played playlist in the system.
		/// Use ToFullPath with PathKind.LocalUser to get the local path and PathKind.RemoteClient to get the cloud path.
		/// </summary>
		public static readonly string CurrentPlaylistPath = "currentPlaylist.xplf";

		/// <summary>
		/// Extension of files in the system that has been marked as deleted with a leading dot
		/// </summary>
		public static readonly string ExtensionDeletedFile = ".deleted";

		/// <summary>
		/// Converts the passed relative path to its absolute or more complete relative representation
		/// </summary>
		/// <param name="path">the relative path to convert</param>
		/// <param name="pathKind">the kind of the relative path to convert</param>
		/// <param name="fileHandler">the file handler object to use. Only needed before the application has been initialized</param>
		/// <returns></returns>
		public static string ToFullPath( string path, PathKind pathKind, IFileHandler fileHandler = null )
		{
			if ( path.StartsWith( "/" ) )
			{
				path = path.Substring( 1 );
			}

			return GetBasePathOf( pathKind, fileHandler ) + "/" + path;
		}

		/// <summary>
		/// Returns the base path of the relative paths of the passed kind
		/// </summary>
		/// <param name="pathKind">the kind of the relative path to check</param>
		/// <param name="fileHandler">the file handler object to use. Only needed before the application has been initialized</param>
		/// <returns></returns>
		public static string GetBasePathOf( PathKind pathKind, IFileHandler fileHandler = null )
		{
			switch ( pathKind )
			{
				case PathKind.LocalSystem:
					return new Uri( Controller.InstallDir, "Data" ).LocalPath;

				case PathKind.LocalUser:
					return (fileHandler ?? ServiceCenter.FileHandler).GetUserDir();

				case PathKind.RemoteSystem:
					return CloudManager.SystemRootDir;

				case PathKind.RemoteClient:
					return CloudManager.ClientsRootDir + "/" + Controller.MachineId;

				default:
					throw new InvalidOperationException( Strings.ExceptionPathKindInvalid );
			}
		}

		/// <summary>
		/// Returns the path of the file that marks the passed file as deleted
		/// </summary>
		/// <param name="filePath"></param>
		/// <returns></returns>
		public static string ToDeletedPath( string filePath )
		{
			return filePath + ExtensionDeletedFile;
		}
	}
}
